<?php
/**
 * 活动管理
 *
 *
 *
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class auto_get_mobile_locationControl{
    public function __construct(){
        register_shutdown_function(array($this,"shutdown"));
    }
    
    //写两个交换数据的临时变量
    //protected   $member_id=0;
    protected  $record=0;
    protected $temp=0; 
    
    public function indexOp(){
        //读文件
        //echo 'start';
        $file_path = "/root/record.txt";
        $str='';
        if(file_exists($file_path)){
            $str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
            
        }else{
            echo "不存在";
        }
        $this->record=$str;
        if ($this->record > $this->temp){
            $this->get_localtionOp();
        }
           
        else if ($this->record==0){
            $this->get_localtionOp();
        }
        else {echo "执行完毕";}
              
    }
    
    public function get_localtionOp(){
        $member_model=Model('member');
        if ($this->record==0){
            $start=0;
        }else{
            $start=$this->record *50;
        }
        
        $sql='select * from lxs_member where mobile_attribution is null order by member_id desc limit '.$start.',50';
        //return $this->table('goods')->limit($limit)->select();

        $member_list= Model()->query($sql);
        foreach ($member_list as $key =>$val){
            //var_export($member_info);
            $this->temp=$this->record;
            $url='http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel='. $val['member_mobile'].'&t='.time();;
            //$content = $this->get_mobileArea($val['member_mobile']);
            $content=$this->get_mobileArea($val['member_mobile']);
            //__GetZoneResult_ = { mts:'1773660', province:'广西', catName:'中国电信', telString:'17736600405', areaVid:'30518', ispVid:'138238560', carrier:'广西电信' } 	             //$aa=$content->__GetZoneResult_;
            // 	            //echo $content;
            //echo $content;
            preg_match_all("/(\w+):'([^']+)/", $content, $m);
            $mobile = array_combine($m[1], $m[2]);
            $mobile_location=$mobile['carrier'];
            $condition['member_id']=$val['member_id'];
            $data['mobile_attribution']=$mobile_location;
            //$result=Model()->table('member')->update($data,$condition);
            
            $result=$member_model->editMember($condition, $data);
            
        }
       
        $this->record=$this->record+1;
        
        file_put_contents('/root/record.txt',$this->record);

        

    }
    
    function get_mobileArea($mobile){
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile."&t=".time();
        $content = file_get_contents($url);
        return mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');
    }
    
    /**
     * 抓取网页
     * @param unknown $url
     */
    public function get_mobile_area($mobile){
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile;
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //函数中加入下面这条语句
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        if (curl_errno($ch)) {
            print curl_error($ch);
        }
        $output = mb_convert_encoding($output, 'utf-8', 'GBK,UTF-8,ASCII');
       
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        return ($output);
    }
}